﻿using Process.BLL;
using Process.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Process.Infastructure
{
    public class ProcessRepository<T> : IProcessRepository<T> where T : Entity
    {

        ProcessDbContext _context;

        public ProcessRepository(ProcessDbContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> CreateAsync(T entity)
        {
            await _context.Set<T>().AddAsync(entity);
            await _context.SaveChangesAsync();
            return entity.Id;  // 返回实体的ID，而不是受影响的行数
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public Task<int> DeleteAsync(T entity)
        {
           _context.Set<T>().Remove(entity);    
            return _context.SaveChangesAsync();
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> DeleteRangeAsync(List<T> t)
        {
            _context.Set<T>().RemoveRange(t);
            return _context.SaveChangesAsync();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IQueryable<T> GetAllAsync()
        {
           return _context.Set<T>();
        }
        //修改
        public Task<int> UpdateAsync(T entity)
        {
            _context.Set<T>().Update(entity);
            return _context.SaveChangesAsync();
        }
    }
}
